package com.ikas.ai.utils;

import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.fop.svg.PDFTranscoder;

import java.io.*;

/**
 * svg转换工具类(以下方法开发足够用了)
 * @param svg
 * @param pdf
 * @throws IOException
 * @throws TranscoderException
 */
@SuppressWarnings("all")
public class SvgUtil {

	public static void main(String[] args) throws Exception {
		/*File svg = new File("D:\\upload\\20211231\\1111.svg");
		File img = new File("D:\\upload\\20211231\\1111.png");
		boolean b = convertSvgFile2Png(svg, img);
		System.out.println(b);*/
	}

	/**
	 * svg文件转成pdf
	 * @param svg
	 * @param pdf
	 * @throws IOException
	 * @throws TranscoderException
	 */
	public static boolean convertSvgFile2Pdf(File svg, File pdf)
	{
		try{
			InputStream in = new FileInputStream(svg);
			OutputStream out = new FileOutputStream(pdf);
			out = new BufferedOutputStream(out);
			convert2Pdf(in, out);
			return true;
		}catch (IOException io){
			io.printStackTrace();
		}catch (TranscoderException te){
			te.printStackTrace();
		}
		return false;
	}

	public static void convert2Pdf(InputStream in, OutputStream out)throws IOException, TranscoderException
	{
		Transcoder transcoder = new PDFTranscoder();
		try {
			TranscoderInput input = new TranscoderInput(in);
			try {
				TranscoderOutput output = new TranscoderOutput(out);
				transcoder.transcode(input, output);
			} finally {
				out.close();
			}
		} finally {
			in.close();
		}
	}

	/**
	 * svg转为png
	 * @param svg
	 * @param pdf
	 * @throws IOException
	 * @throws TranscoderException
	 */
	public static boolean convertSvgFile2Png(File svg, File png)
	{
		try {
			InputStream in = new FileInputStream(svg);
			OutputStream out = new FileOutputStream(png);
			out = new BufferedOutputStream(out);
			convert2PNG(in, out);
			return true;
		}catch (IOException io){
			io.printStackTrace();
		}catch (TranscoderException te){
			te.printStackTrace();
		}
		return false;
	}


	public static void convert2PNG(InputStream in, OutputStream out)throws IOException, TranscoderException
	{
		Transcoder transcoder = new PNGTranscoder();
		try {
			TranscoderInput input = new TranscoderInput(in);
			try {
				TranscoderOutput output = new TranscoderOutput(out);
				transcoder.transcode(input, output);
			} finally {
				out.close();
			}
		} finally {
			in.close();
		}
	}

	/**
	 * 字符串转成pdf
	 * @param svg
	 * @param pdf
	 * @throws IOException
	 * @throws TranscoderException
	 */
	public static void convertStr2Pdf(String svg, File pdf) throws IOException,TranscoderException 
	{
		InputStream in = new ByteArrayInputStream(svg.getBytes());
		OutputStream out = new FileOutputStream(pdf);
		out = new BufferedOutputStream(out);
		convert2Pdf(in, out);
	}

	/**
	 * 将svg字符串转换为png
	 *
	 * @param svgCode svg代码
	 * @param pngFilePath 保存的路径
	 * @throws TranscoderException svg代码异常
	 * @throws IOException io错误
	 */
	public static void convertToPng(String svgCode, String pngFilePath) throws IOException,
			TranscoderException {
		File file = new File(pngFilePath);
		FileOutputStream outputStream = null;
		try {
			file.createNewFile();
			outputStream = new FileOutputStream(file);
			convertToPng(svgCode, outputStream);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 将svgCode转换成png文件，直接输出到流中
	 *
	 * @param svgCode svg代码
	 * @param outputStream 输出流
	 * @throws TranscoderException 异常
	 * @throws IOException io异常
	 */
	public static void convertToPng(String svgCode, OutputStream outputStream)
			throws TranscoderException, IOException {
		try {
			byte[] bytes = svgCode.getBytes("utf-8");
			PNGTranscoder t = new PNGTranscoder();
			TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
			TranscoderOutput output = new TranscoderOutput(outputStream);
			t.transcode(input, output);
			outputStream.flush();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}